package com.youease.chris.view.mediator
{
	import com.youease.chris.MyAppFacade;
	import com.youease.chris.control.CreateProjectCommand;
	import com.youease.chris.control.OpenProjectCommand;
	import com.youease.chris.control.CreateMapCommand;
	import com.youease.chris.control.MenuBarCommand;
	import com.youease.chris.model.proxy.MenuDataProxy;
	import com.youease.chris.model.vo.MenuData;
	
	import mx.events.MenuEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class MapEditMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "MapEditMediator";
		
		private var menuDataProxy:MenuDataProxy;
		
		public function MapEditMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
			mapEdit.menuBar.addEventListener(MenuEvent.ITEM_CLICK,onMenubarClick);
		}
		
		public function onMenubarClick(evt:MenuEvent):void {
			sendNotification(MenuBarCommand.MENU_CHANGE,evt.item);
		}
		
		override public function listNotificationInterests():Array{
			//列出感兴趣的Notification
			return [
				MyAppFacade.APP_STARTUP_OVER,
				CreateProjectCommand.CREATE_PROJECT_COMPLETE,
				OpenProjectCommand.OPEN_PROJECT_COMPLETE,
				CreateMapCommand.CREATE_MAP_COMPLETE
			];
		}	
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case MyAppFacade.APP_STARTUP_OVER:
					menuDataProxy = this.facade.retrieveProxy(MenuDataProxy.NAME) as MenuDataProxy;
					mapEdit.menuBar.dataProvider = menuDataProxy.menuData.menuBarCollection;
					this.facade.registerCommand(MenuBarCommand.MENU_CHANGE,MenuBarCommand);
					break;
				case CreateProjectCommand.CREATE_PROJECT_COMPLETE:
					menuDataProxy.enableMenuItem("saveProject");
					menuDataProxy.enableMenuItem("newMap");
					break;
				case OpenProjectCommand.OPEN_PROJECT_COMPLETE:
					menuDataProxy.enableMenuItem("saveProject");
					menuDataProxy.enableMenuItem("newMap");
					break;
				case CreateMapCommand.CREATE_MAP_COMPLETE:
					mapEdit.mapProperties.visible = true;
					mapEdit.tools.visible = true;
					menuDataProxy.enableMenuItem("newTexture");
					menuDataProxy.enableMenuItem("textureView");
					break;
			}
		}
		
		
		public function get mapEdit():MapEdit {
			return this.viewComponent as MapEdit;
		}
	}
}